package com.financing.wap.web.controller.api;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.service.IRecommentProductService;
import com.financing.wap.vo.RecommentProductVO;
import com.financing.wap.web.controller.BaseController;

@Controller("apiRecommentController")
@RequestMapping("/api/recommentProduct")
public class RecommentProductController extends BaseController {
	
	Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	IRecommentProductService recommentProductService;

	/**
	 * 查询推荐产品
	 * @return
	 */
	@RequestMapping("/queryRecommentProduct")
	@ResponseBody
	public ResultMap queryRecommentProduct(HttpServletRequest request, HttpServletResponse response,String recommentType ){
		ResultMap result = new ResultMap();
		try {
			//默认查询热销产品
			if(StringUtils.isBlank(recommentType)){
				recommentType="1";
			}
			
			List<RecommentProductVO> queryRecommentProduct = recommentProductService.queryRecommentProduct(recommentType);
			
			Collections.sort(queryRecommentProduct,new Comparator<RecommentProductVO>() {
				@Override
				public int compare(RecommentProductVO o1, RecommentProductVO o2) {
					return o2.getAddDate().compareTo( o1.getAddDate());
				}
			});
			result.setAttribute("list", queryRecommentProduct);
			result.setAttribute("fyFlag",getFYflag());
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (SQLException e) {
			result.setStatus(STATUS.FAILE);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
}
